// pages/wb/upload.js
const common = require('../../utils/mr_xu/common.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        files: [],
        uploadedFiles: [],
        count: 4,
    },


    onShow() {
        const data = getApp().getTmpPlate();
        console.log(data);
        this.setData({
            data,
            count: data.carType === '新车' ? 4 : 1,
        });
    },
    toStep() {
        const data = getApp().getTmpPlate();

        if (data.carType === '新车') {
            if (this.data.uploadedFiles.length != 4) {
                return wx.showToast({
                    icon: 'none',
                    title: '新车至少上传4张图片',
                })
            }

        } else {
            if (this.data.uploadedFiles.length != 1) {
                return wx.showToast({
                    icon: 'none',
                    title: '旧车至少上传1张图片',
                })
            }
        }
        getApp().setTmpPlate({
            files: this.data.uploadedFiles
        })
        wx.navigateTo({
            url: './concat-msg',
        })
    },
    chooseImage: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        console.log(that.data.count - that.data.files.length);
        wx.chooseImage({
            count: that.data.count - that.data.files.length,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                if (res.tempFilePaths.length > that.data.count) {
                    return wx.showToast({
                        title: `最多只能上传${that.data.count}个文件`,
                    })
                }
                that.setData({
                    files: that.data.files.concat(res.tempFilePaths)
                });
                that.upload(res.tempFilePaths);
            }
        })
    },

    deleteImg(e) {
        wx.showModal({
            title: '提示',
            content: '是否删除该图片？',
            success: res => {
                const {
                    files
                } = this.data;
                files.splice(e.target.dataset.index, 1);
                this.setData({
                    files,
                })
            }
        })
    },

    upload(file) {
        const arr = typeof file === 'string' ? [file] : file;
        wx.showLoading({
            title: '上传中，请稍后...',
        });

        function doUpload() {
            const first = arr.pop();
            if (!first) {
                wx.hideLoading();
                return;
            }
            common.upload(first, url => {
                console.log(url);
                const {
                    uploadedFiles
                } = this.data;
                uploadedFiles.push(url);
                this.setData({
                    uploadedFiles
                })
                doUpload.call(this)
            });
        }
        doUpload.call(this);
    }
})